import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.JTextField;


public class TestPanel extends JScrollPane {
	private static TestPanel gi = null;
	public static TestPanel Gi() { return gi; } 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Map<JComponent,CompInfo> mKids = new HashMap<JComponent,CompInfo>();
	
	ArrayList<JComponent> mComps = new ArrayList<JComponent>();
	ArrayList<CompInfo> mInfos = new ArrayList<CompInfo>();
	
	JButton mComp1 = new JButton("Button1");
	JButton mComp2 = new JButton("Button2");
	JTextField mComp3 = new JTextField("Text1");
	JTextField mComp4 = new JTextField("Text2");
	
	
	public TestPanel(){
		gi = this;
		
		int w = 4;
		this.setBorder( BorderFactory.createMatteBorder(
				w, w, w, w, Color.YELLOW));
		this.setLayout(null);
		//
		int y0=100;
		int ys = 100;
		for(int i=0; i< 4; i++){
			mInfos.add(new CompInfo());
			mInfos.get(i).setLocation(new Point(100, y0+ys*i));
			mInfos.get(i).setSize(new Dimension(100, 50));
		}
		
		mComps.add(mComp1);	
		mComps.add(mComp2);
		mComps.add(mComp3);
		mComps.add(mComp4);
		
	}
	
	@Override
	public void paint(Graphics g){
		super.paint(g);
		// paint test message;
		Dimension size = this.getSize();
		g.drawString("Hell, TestPanel", size.width/3 , size.height/3);
		// paint kids;
		this.paintKids(g);
	}
	public void paintKids(Graphics g){
		for(int i=0; i<mComps.size();i++){
			if(!mInfos.get(i).isActive()){
				Point p = mInfos.get(i).getLocation();
				g.translate(p.x, p.y);
				mComps.get(i).paint(g);
				g.translate(-p.x, -p.y);
			}
		}
	}
	
	public boolean isActive(int idx){
		return mInfos.get(idx).isActive();
	}
	public void addKid(int idx, boolean active){
		JComponent c = mComps.get(idx);
		if( active ) {
			this.add(c);
			c.setLocation(mInfos.get(idx).getLocation());
			c.setSize(mInfos.get(idx).getSize());
		}
		else 
			this.remove(c);
		// 
		CompInfo ci = mInfos.get(idx);
		ci.setActive(active);
		System.out.println("[TestPanel].addKid"+idx+active);
		//
		repaint();
	}
}
